package com.yangcangclub.message.domain;

import com.yangcangclub.message.bean.PrivMsgBean;
import com.google.common.collect.Maps;
import com.yangcangclub.commons.exception.ApolloException;
import com.yangcangclub.commons.result.MessageEnum;
import com.yangcangclub.commons.utils.IdWorker;
import com.yangcangclub.message.base.MesgBaseDomain;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collections;
import java.util.List;
import java.util.Map;

/**
 * 私信
 */
@Component
public class PrivateMsg  extends MesgBaseDomain{


    /**
     *发送私信
     * 返回消息id，失败则为0
     */
    public int send(int userId,long shareMsgId,String content){
        Map<String,Object> map= Maps.newHashMap();
        map.put("user_id",userId);
        map.put("share_msg_id",shareMsgId);
        map.put("content",content);
        map.put("id",0);
        //新增
       super.getMasterJdbc().insert("message.MesgMapper.insertPrivateMessage", map);

        return (int)map.get("id");

    }

    /**
     * 更新消息阅读数
     * @return
     */
    public boolean resetReadNum(long shareMsgId,int userId){
        Map<String,Object> map= Maps.newHashMap();
        map.put("user_id",userId);
        map.put("share_msg_id",shareMsgId);
        return 0<= super.getMasterJdbc().update("message.MesgMapper.resetUnreadNum",map);
    }

    /**
     * 未读消息数+1
     * @return
     */
    public boolean addUnReadNum(int userId,int followid){
        Map<String,Object> map= Maps.newHashMap();
        map.put("user_id",userId);
        map.put("follow_user_id",followid);
        return 1==super.getMasterJdbc().update("message.MesgMapper.updateUnreadNum",map);
    }

    /**
     * 查询会话属性
     * @return
     */
    public Map<String,Object> queryMsgSessionInfo(int userId,int followid){
        Map<String,Object> map= Maps.newHashMap();
        map.put("user_id",userId);
        map.put("follow_user_id",followid);
        return super.getSlaveJdbc().selectOne("message.MesgMapper.selectPrivMsgRelation",map);
    }

    /**
     * 查询私信消息内容
     * @return
     */
    public PrivMsgBean queryPrivateMsg(int msgId){
        return super.getSlaveJdbc().selectOne("message.MesgMapper.selectPrivateMessage",msgId);
    }

    /**
     * 查询未读私信消息数量
     * @return
     */
    public int selectPrivMsgUnreadNum(int userId){
       Object obj= super.getSlaveJdbc().selectOne("message.MesgMapper.selectPrivMsgUnreadNum",userId);
        return obj==null?0:(int)obj;
    }


    /**
     *获取对话详情列表
     */
    public List<PrivMsgBean> readList(long shareMsgId,int beginId,int size){

        Map<String,Object> map= Maps.newHashMap();
        if(beginId!=0){
            map.put("id",beginId);
        }
        map.put("size",size);
        map.put("share_msg_id",shareMsgId);
        List<PrivMsgBean> list= super.getSlaveJdbc().selectList("message.MesgMapper.selectPrivateMsgList", map);
        
        if(list!=null&&beginId==0){//反转顺序
            Collections.reverse(list);
        }
        return list;
    }


    /**
     *获取已建立过的会话总数
     */
    public int  sessionSize(int userId){
        return super.getSlaveJdbc().selectOne("message.MesgMapper.selectUserListCount", userId);
    }

    /**
     *获取已建立过的会话列表
     */
    public List<Map<String,Object>>  getUserList(int userId,int begin,int size,String isAll){
        Map<String,Object> map= Maps.newHashMap();
        map.put("user_id",userId);
        map.put("size",size);
        map.put("start",begin);
        if("N".equals(isAll)) map.put("isAll",isAll);

        return super.getSlaveJdbc().selectList("message.MesgMapper.selectUserList", map);
    }



    /**
     *建立私信会话关系
     * 返回会话id
     */
    @Transactional(rollbackFor= Exception.class)
    public long  buildSession(int userId,int followId){
        Map<String,Object> map= Maps.newHashMap();
        map.put("user_id",userId);
        map.put("follow_user_id",followId);
        Map<String,Object> temp= super.getSlaveJdbc().selectOne("message.MesgMapper.selectPrivMsgRelation",map);
        if(temp==null){//不存在新增
            String id=""+new IdWorker(3).nextId();
            id=id.substring(id.length()-16);
            map.put("share_msg_id",id);
           boolean b1=1== super.getMasterJdbc().insert("message.MesgMapper.insertPrivMsgRelation", map);
            //调换顺序
            map.put("user_id",followId);
            map.put("follow_user_id",userId);
           boolean b2=1== super.getMasterJdbc().insert("message.MesgMapper.insertPrivMsgRelation", map);
            if( !b1|| !b2){
                throw new ApolloException(MessageEnum.DATABASE_ERROR);
            }

            return Long.valueOf(id);
        }

        return (Long)temp.get("share_msg_id");
    }


}
